package main

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"go.uber.org/dig"
	"log"
	"shop/src/controller"
	"shop/src/launch"
	"shop/src/porter"
	"shop/src/repository"
	"shop/src/service"
)

func main() {
	inject()

	app := iris.New()
	app.Logger().SetLevel("debug")
	mvc.Configure(app.Party("/RegisterOrders"), registerOrders)

	err := app.Run(
		iris.Addr("0.0.0.0:8080"),
		iris.WithoutServerError(iris.ErrServerClosed),
		iris.WithOptimizations,
	)
	if err != nil {
		log.Fatal("服务启动失败")
	}
}

func inject() {
	c := dig.New()
	err := c.Provide(launch.Xorm)
	if err != nil {
		panic(err)
	}
	err = c.Provide(service.NewOrderService)
	if err != nil {
		panic(err)
	}
	err = c.Provide(repository.NewOrderRepository)
	if err != nil {
		panic(err)
	}
	err = c.Provide(repository.NewDocumentRepository)
	if err != nil {
		panic(err)
	}
	err = c.Provide(repository.NewCheckpointRepository)
	if err != nil {
		panic(err)
	}
	err = c.Provide(porter.NewSynchronizer)
	if err != nil {
		panic(err)
	}
	if err != nil {
		panic(err)
	}
	err = c.Invoke(func(s *service.OrderService) {})
	if err != nil {
		panic(err)
	}
	err = c.Invoke(func(r *repository.OrderRepository) {})
	if err != nil {
		panic(err)
	}
}

func registerOrders(app *mvc.Application) {
	app.Handle(new(controller.OrderController))
}
